package com.codename1.samples;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Button;
import static com.codename1.ui.ComponentSelector.$;
import com.codename1.ui.Container;
import com.codename1.ui.animations.CommonTransitions;
import com.codename1.ui.layouts.BorderLayout;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class FadeOutTransitionSample {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", new BorderLayout());
        Button button1 = new Button("Button 1");
        $(button1).selectAllStyles().setBgColor(0xff0000);
        
        Button button2 = new Button("Button 2");
        $(button2).selectAllStyles().setBgColor(0x00ff00);
        
        Button doFade = new Button("Toggle");
        doFade.addActionListener(evt->{
            Container contentPane = hi.getContentPane();
            if (contentPane.contains(button1)) {
                button2.remove();
                Container wrapper = BorderLayout.center(button2);
                
                contentPane.replace(button1.getParent(), wrapper, CommonTransitions.createFade(500));
            } else if (contentPane.contains(button2)) {
                Container empty = new Container();
                $(empty).selectAllStyles().setBgColor(0xeaeaea).setBgTransparency(0xff);
                
                contentPane.replace(button2.getParent(), empty, CommonTransitions.createFade(500));
                //contentPane.removeComponent(empty);

            } else {
                Container empty = new Container();
                button1.remove();
                contentPane.add(CENTER, empty);
                contentPane.revalidateWithAnimationSafety();
                contentPane.replace(empty, BorderLayout.center(button1), CommonTransitions.createFade(500));
            }
        });
        
        hi.add(NORTH, doFade);
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
